package com.tanhua.listener;

import com.tanhua.domain.Log;
import com.tanhua.mapper.LogMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-logs",consumerGroup = "tanhua-logs-group")
public class LogMessageListener implements RocketMQListener<Map> {

    @Autowired
    private LogMapper logMapper;


    @Override //监听中间件 只要有内容 就执行
    public void onMessage(Map message) {
        //1 获取消息中间件的内容
        Integer userId=(Integer)message.get("userId");
        String type = (String) message.get("type");
        String logTime = (String) message.get("logTime");
        //2 插入内容数据到数据库
        Log log = new Log();

        log.setLogTime(logTime);
        log.setUserId(userId.longValue());
        log.setType(type);

        System.out.println("消费者接受到了消息："+log);

        logMapper.insert(log);


    }
}
